<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>角色管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 编辑模态框 -->
    <el-dialog title="提示" :visible.sync="dialogVisiblebj" width="30%">
      <el-form
        :model="ruleFormbj"
        :rules="rulesbj"
        ref="ruleFormbj"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="ruleFormbj.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="ruleFormbj.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisiblebj = false">取 消</el-button>
        <el-button type="primary" @click="submitFormbj('ruleFormbj')">确 定</el-button>
      </span>
    </el-dialog>
    <!--  -->

    <!-- 添加角色模态框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="ruleForm.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色名称" prop="roleDesc">
          <el-input v-model="ruleForm.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
    <!--  -->
    <el-card style="margin-top: 10px">
      <el-button type="primary" @click="dialogVisible = true">添加角色</el-button>

      <el-table :data="roles" style="width: 100%">
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-row  v-for="item in props.row.children" :key="item.id">
              <el-col :span="5">
                <el-tag closable effect="dark" @close='delone(props.row,item.id)'> 
                    {{ item.authName }}
                </el-tag>
              </el-col>
              <el-col :span="19">
                  <el-row v-for="item1 in item.children"
                  :key='item1.id'
                  >
                      <el-col :span='5'>
                          <el-tag closable effect='dark' type='warning' @close='delone(props.row,item1.id)'>
                             {{ item1.authName }}
                          </el-tag>
                      </el-col>
                      <el-col :span='19'>
                          <el-tag v-for="item2 in item.children"
                          :key='item2.id' closable type='info' effect='dark' @close='delone(props.row,item2.id)'
                          >
                           {{ item2.authName }}
                          </el-tag>
                      </el-col>
                  </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column type="index" prop="#"> </el-table-column>
        <el-table-column label="角色名称" prop="roleName"> </el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"> </el-table-column>
        <el-table-column label="操作" prop="desc">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="edit(scope.row)"
              >编辑</el-button
            >
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="del(scope.row.id)"
              >删除</el-button
            >
            <el-button type="warning" size="mini" icon="el-icon-s-tools"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      roles: [],
      dialogVisible: false,
      ruleForm: {
        roleName: "",
        roleDesc: "",
      },
      rules: {
        roleName: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        roleDesc: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
      },
      dialogVisiblebj: false,
      ruleFormbj: {
        roleName: "",
        roleDesc: "",
      },
      rulesbj: {
        roleName: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        roleDesc: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
      },
    };
  },
  mounted() {
    this.getdata();
  },
  methods: {
      delone(row,rightId){
          this.$axios.delete(`roles/${row.id}/rights/${rightId}`)
          .then(res=>{
              console.log(res,8989);
              if(res.meta.status==200){
                  this.$message.success('删除成功')

                  row.children=res.data
              }
          })
      },
    edit(row) {
      this.dialogVisiblebj = true;
      this.ruleFormbj = row;
    },
    submitFormbj(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.put("roles/" + this.ruleFormbj.id, this.ruleFormbj).then((res) => {
            if (res.meta.status == 200) {
              this.$message.success("编辑成功");
              this.getdata();
              this.dialogVisiblebj = false;
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    del(id) {
      this.$axios.delete("roles/" + id).then((res) => {
        if (res.meta.status == 200) {
          console.log(res, 8888);
          this.$message.success("删除成功");
          this.getdata();
        }
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$axios.post("roles", this.ruleForm).then((res) => {
            if (res.meta.status == 201) {
              this.$message.success("添加成功");
              this.getdata();
              this.dialogVisible = false;
              this.ruleForm.roleName = "";
              this.ruleForm.roleDesc = "";
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getdata() {
      this.$axios.get("roles").then((res) => {
        this.roles = res.data;
      });
    },
  },
};
</script>

<style lang="less" scoped>
.el-row{
  width:90%;
  margin:5px auto;
  border:1px solid #ddd;
}
.el-col{
  padding:10px;
  // border-right:1px solid #ddd;
}
.el-tag{
  margin-right:10px;
  margin-top:10px;
}
</style>
